{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  Python 中的文件操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " python中对于数据输入输出的处理和常规性语言类似。主要包括:系统输入输出、文件存储、数据库存储等，其中数据存储读取操作中主要就是对于文件和数据库的处理方面。python中对于文件的处理提供了一个比较常用且比较方便的内建类file，通过file可以比较方便的对文件操作。python中对于文件的操作较多的方法可以很方便的实现文件的读取、写入、删除、查找等操作，如下示例:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "蔡海飞\n",
      "\n",
      "123\n",
      "file name: testFile.txt\n",
      "456\n",
      "\n",
      "pwd\n",
      "\n",
      "87\n",
      "File not open for writing\n",
      "line: 蔡海飞\n",
      "\n",
      "line: 123456\n",
      "\n",
      "line: pwd\n",
      "\n",
      "line: 男\n",
      "\n",
      "line: 1992print \"append mode------------------------------2 \"print \"append mode------------------------------2 \"\n",
      "蔡海飞\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "蔡海飞\n",
      "123456\n",
      "pwd\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/env python\n",
    "#-*-encoding:utf-8-*-\n",
    "\n",
    "import  sys,fileinput,codecs\n",
    "\n",
    "testFile = file('testFile.txt')\n",
    "# 文件读操作\n",
    "# print testFile.read()\n",
    "# 文件单行读操作.\n",
    "print testFile.readline()\n",
    "# 文件单行制定字符读操作.\n",
    "print testFile.readline(3)\n",
    "# 文件读所有行,并返回一个list.\n",
    "# print testFile.readlines()\n",
    "print \"file name:\",testFile.name\n",
    "# 文件迭代\n",
    "print testFile.next()\n",
    "print next(testFile)\n",
    "\n",
    "# 返回一个整型的文件描述符,可供底层操作系统调用.\n",
    "print testFile.fileno()\n",
    "\n",
    "try:\n",
    "    testFile.write(\"test\")\n",
    "except Exception as e:\n",
    "    print e\n",
    "\n",
    "# 关闭文件\n",
    "testFile.close()\n",
    "\n",
    "# with  open(name=\"testFile.txt\",mode='wb+',buffering=1024) as testFile:\n",
    "#     testFile.write(\"print \\\"append mode------------------------------ 1\\\"\")\n",
    "#     print testFile.read()\n",
    "\n",
    "with  open(name=\"testFile.txt\",mode='a+') as testFile:\n",
    "    testFile.write(\"print \\\"append mode------------------------------2 \\\"\")\n",
    "    testFile.flush()\n",
    "    testFile.seek(0)\n",
    "    for line in testFile:\n",
    "        print \"line:\",line\n",
    "\n",
    "# fileinput\n",
    "testFile =  fileinput.input('testFile.txt') \n",
    "for line in testFile:\n",
    "    print line\n",
    "    testFile.close()\n",
    "    break\n",
    "    \n",
    "# print testFile.filename()\n",
    "\n",
    "# codecs\n",
    "with codecs.open('testFile.txt','rb',encoding='utf-8') as testFile:\n",
    "    print '\\n\\n\\n'\n",
    "    print testFile.read(10)\n",
    "\n",
    "# print help(open)\n",
    "# print file.__doc__\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "python文件操作，打开文件可以通过指定文件操作的模式类型从而赋予文件操作的不同权限,python中对于文件的模式甚至如下几个类型:\n",
    "\n",
    "- r  \n",
    "  模式'r'为读模式，一般不制定文件打开的模式时默认为读模式\n",
    " \n",
    "- w  \n",
    "  模式'w'为写模式\n",
    "\n",
    "- b\n",
    "   模式’b‘为以二进制格式打开文件,该模式一般会和其他模式混合使用如:'rb'、’wb‘,'ab'等\n",
    "   \n",
    " - a   \n",
    "   模式'a'为追加模式，一般特指写模式的追加，一般的写模式'w'执行写操作时若文件存在则直接覆盖(指针指向开始位置)，若不存在则创建，而'a'模式下的文件写操作则是若文件存在，则指针指向文件结尾追加写入，若文件不存在则创建。\n",
    "  \n",
    "同时允许通过在模式后添加一个'+'来来实现文件的读写。如'r+'、'rb+'、'w+'、’wb+‘、’a+‘、’ab+‘。文件打开还有一个参数是设置文件的buffering的，当buffering为0表示不设置缓存，buffering为1则表示缓存为一行，当其他大于1的数字则表示缓存的大小。如上，文件的打开可以使用系统自带的内置类file或者内置函数open函数，也可以通过fileinput模块或者codecs(可以对文件打开设置编码)模块进行打开。\n",
    "  \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[enjoytoday](http://www.enjoytoday.cn)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
